﻿using System;
using System.Collections.Generic;
using NHibernate.Mapping.ByCode;
using Xenta.Enums;

namespace Xenta.Entities
{
    /// <summary>
    /// Represents the topic entity.
    /// </summary>
    /// <remarks><pre>
    /// The entity regarding the disclosure level.
    /// 
    /// + Entire
    ///  + Extended
    ///   | - Description
    ///   + Base
    ///    | - Forum(of the Primary level)
    ///    | - LastPost(of the Base level)
    ///    | - Member(of the Base level)
    ///    | - Title
    ///    | - PostCount
    ///    | - Flags
    ///    | - UpdatedOn
    ///    | - CreatedOn
    ///    + Keys
    ///     + Primary
    ///      | - EntityID
    /// 
    /// Note: upper level includes lower level.
    /// </pre></remarks>
    public class ForumTopicEntity : AuditableEntity
    {
        #region Fields

        private ForumPostEntity _lastPost;
        private ICollection<ForumPostEntity> _posts;

        #endregion

        #region Ctors

        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public ForumTopicEntity()
        {
            _lastPost = null;
            _posts = new List<ForumPostEntity>();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the forum.
        /// </summary>
        public virtual ForumEntity Forum
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the member.
        /// </summary>
        public virtual ForumMemberEntity Member
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the last post identifier.
        /// </summary>
        public virtual int LastPostID
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the last post.
        /// </summary>
        public virtual ForumPostEntity LastPost
        {
            get
            {
                return LastPostID != 0 ? _lastPost : null;
            }
            set
            {
                _lastPost = value;
                LastPostID = value != null ? value.EntityID : 0;
            }
        }

        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        public virtual string Title
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        public virtual string Description
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the post count.
        /// </summary>
        public virtual int PostCount
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the flags.
        /// </summary>
        public virtual ForumTopicFlags Flags
        {
            get;
            set;
        }

        #endregion

        #region Collections

        /// <summary>
        /// Gets or sets the post collection.
        /// </summary>
        public virtual ICollection<ForumPostEntity> Posts
        {
            get
            {
                return _posts;
            }
            set
            {
                _posts = value;
            }
        }

        #endregion

        #region Utilities

        protected override void Disclose(IDictionary<String, Object> obj,
            DisclosureLevel level,
            IDictionary<String, String> options)
        {
            base.Disclose(obj, level, options);
            if(level >= DisclosureLevel.Base)
            {
                obj["Forum"] = Forum.Disclose(DisclosureLevel.Primary, options);
                obj["LastPost"] = LastPost != null
                    ? LastPost.Disclose(DisclosureLevel.Base, options)
                    : null;
                obj["Member"] = Member.Disclose(DisclosureLevel.Base, options);
                obj["Title"] = Title;
                obj["PostCount"] = PostCount;
                obj["Flags"] = Flags.ToString();
            }
            if(level >= DisclosureLevel.Extended)
                obj["Description"] = Description;
        }

        #endregion
    }

    /// <summary>
    /// The entity NHibernate mapping.
    /// </summary>
    public class ForumTopicMapping : EntityMapping<ForumTopicEntity>
    {
        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public ForumTopicMapping()
        {
            Table("fwk_ForumTopics");
            Id(x => x.EntityID, m =>
            {
                m.Column("TopicID");
                m.Generator(Generators.Identity);
            });
            ManyToOne(x => x.Forum, m =>
            {
                m.Column("ForumID");
                m.ForeignKey("FK_fwk_ForumTopics_fwk_Forums");
                m.NotNullable(true);
                m.Fetch(FetchKind.Join);
            });
            ManyToOne(x => x.Member, m =>
            {
                m.Column("MemberID");
                m.ForeignKey("FK_fwk_ForumTopics_fwk_ForumMembers");
                m.NotNullable(true);
                m.Fetch(FetchKind.Join);
            });
            Property(x => x.LastPostID, m =>
            {
                m.NotNullable(true);
            });
            ManyToOne(x => x.LastPost, m =>
            {
                m.Column("LastPostID");
                m.NotFound(NotFoundMode.Ignore);
                m.Insert(false);
                m.Update(false);
            });
            Property(x => x.Title, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.Description, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.PostCount, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.Flags, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.CreatedOn, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.UpdatedOn, m =>
            {
                m.NotNullable(true);
            });
            Bag(x => x.Posts, m =>
            {
                m.Table("fwk_ForumPosts");
                m.Key(k =>
                {
                    k.Column("TopicID");
                    k.ForeignKey("FK_fwk_ForumPosts_fwk_ForumTopics");
                    k.NotNullable(true);
                });
                m.Inverse(true);
                m.BatchSize(10);
            },
            a =>
            {
                a.OneToMany();
            });
        }
    }
}
